package com.wkbb.sports.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;


/** 
 * 日期处理工具类 
 * @author dylan_xu 
 * @date Mar 11, 2012 
 * @modified by 
 * @modified date 
 * @since JDK1.6 
 */

@Slf4j
public class DateUtil {  
    // ~ Static fields/initializers  
    // =============================================  
  
    private static String defaultDatePattern = null;
    private static String timePattern = "HH:mm";
    public static final String TS_FORMAT = DateUtil.getDatePattern() + " HH:mm:ss.S";
    /** 日期格式yyyy-MM字符串常量 */  
    private static final String MONTH_FORMAT = "yyyy-MM";  
    /** 日期格式yyyy-MM-dd字符串常量 */  
    private static final String DATE_FORMAT = "yyyy-MM-dd";  
    /** 日期格式HH:mm:ss字符串常量 */  
    private static final String HOUR_FORMAT = "HH:mm:ss";  
    /** 日期格式yyyy-MM-dd HH:mm:ss字符串常量 */  
    private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /** 日期格式yyyy-MM-dd HH:mm:ss字符串常量 */
    private static final String YYMMDD_FORMAT = "yyyyMMdd";
    /** 某天开始时分秒字符串常量  00:00:00 */  
    private static final String DAY_BEGIN_STRING_HHMMSS = " 00:00:00";  
    /**  某天结束时分秒字符串常量  23:59:59  */  
    public static final String DAY_END_STRING_HHMMSS = " 23:59:59";  
    private static SimpleDateFormat sdf_date_format = new SimpleDateFormat(DATE_FORMAT);  
    private static SimpleDateFormat sdf_hour_format = new SimpleDateFormat(HOUR_FORMAT);  
    private static SimpleDateFormat sdf_datetime_format = new SimpleDateFormat(DATETIME_FORMAT);
    private static SimpleDateFormat sdf_yymmdd_format = new SimpleDateFormat(YYMMDD_FORMAT);

    /** 
     * 获得服务器当前日期及时间，以格式为：yyyy-MM-dd HH:mm:ss的日期字符串形式返回 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @return 
     */  
    public static String getDateTime() {  
        try {  
            return sdf_datetime_format.format(Calendar.getInstance().getTime());
        } catch (Exception e) {  
            log.debug("DateUtil.getDateTime():" + e.getMessage());
            return "";  
        }  
    }  
  
    /** 
     * 获得服务器当前日期，以格式为：yyyy-MM-dd的日期字符串形式返回 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @return 
     */  
    public static String getDate() {
        try {  
            return sdf_date_format.format(Calendar.getInstance().getTime());
        } catch (Exception e) {  
            log.debug("DateUtil.getDate():" + e.getMessage());
            return "";  
        }  
    }  
  
    /** 
     * 获得服务器当前时间，以格式为：HH:mm:ss的日期字符串形式返回 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @return 
     */  
    public static String getTime() {  
        String temp = " ";  
        try {  
            temp += sdf_hour_format.format(Calendar.getInstance().getTime());
            return temp;  
        } catch (Exception e) {  
            log.debug("DateUtil.getTime():" + e.getMessage());
            return "";  
        }  
    }  
  
    /** 
     * 统计时开始日期的默认值 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @return 
     */  
    public static String getStartDate() {  
        try {  
            return getYear() + "-01-01";  
        } catch (Exception e) {  
            log.debug("DateUtil.getStartDate():" + e.getMessage());
            return "";  
        }  
    }  
  
    /** 
     * 统计时结束日期的默认值 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @return 
     */  
    public static String getEndDate() {  
        try {  
            return getDate();  
        } catch (Exception e) {  
            log.debug("DateUtil.getEndDate():" + e.getMessage());
            return "";  
        }  
    }  
  
    /** 
     * 获得服务器当前日期的年份 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @return 
     */  
    public static String getYear() {
        return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
    }
  
    /** 
     * 获得服务器当前日期的月份 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @return 
     */  
    public static String getMonth() {
        java.text.DecimalFormat df = new java.text.DecimalFormat();
        df.applyPattern("00;00");
        return df.format((Calendar.getInstance().get(Calendar.MONTH) + 1));
    }  
  
    /** 
     * 获得服务器在当前月中天数 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @return 
     */  
    public static String getDay() {
        return String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
    }  
  
    /** 
     * 比较两个日期相差的天数 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @param date1 
     * @param date2 
     * @return 
     */  
    public static int getMargin(String date1, String date2) {  
        int margin;  
        try {  
            ParsePosition pos = new ParsePosition(0);  
            ParsePosition pos1 = new ParsePosition(0);  
            Date dt1 = sdf_date_format.parse(date1, pos);  
            Date dt2 = sdf_date_format.parse(date2, pos1);  
            long l = dt1.getTime() - dt2.getTime();  
            margin = (int) (l / (24 * 60 * 60 * 1000));  
            return margin;  
        } catch (Exception e) {  
            log.debug("DateUtil.getMargin():" + e.toString());
            return 0;  
        }  
    }  

    /** 
     * 返回日期加X天后的日期 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @param date 
     * @param i 
     * @return 
     */  
    public static String addDay(String date, int i) {  
        try {  
            GregorianCalendar gCal = new GregorianCalendar(  
                    Integer.parseInt(date.substring(0, 4)),   
                    Integer.parseInt(date.substring(5, 7)) - 1,   
                    Integer.parseInt(date.substring(8, 10)));  
            gCal.add(GregorianCalendar.DATE, i);  
            return sdf_date_format.format(gCal.getTime());  
        } catch (Exception e) {  
            log.debug("DateUtil.addDay():" + e.toString());
            return getDate();  
        }  
    }

    //返回当前日期加X天后的日期
    public static String addCurrDay(int i) {
        try {
            Calendar gCal = Calendar.getInstance();
            gCal.add(GregorianCalendar.DATE, i);
            return sdf_date_format.format(gCal.getTime());
        } catch (Exception e) {
            log.debug("DateUtil.addCurrDay():" + e.toString());
            return getDate();
        }
    }

    //返回当前日期加X分钟后的日期
    public static String addCurrMinute(int i) {
        try {
            Calendar gCal = Calendar.getInstance();
            gCal.add(GregorianCalendar.MINUTE, i);
            return sdf_datetime_format.format(gCal.getTime());
        } catch (Exception e) {
            log.debug("DateUtil.addCurrMinute():" + e.toString());
            return getDate();
        }
    }
  
    /** 
     * 返回日期加X月后的日期 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @param date 
     * @param i 
     * @return 
     */  
    public static String addMonth(String date, int i) {  
        try {  
            GregorianCalendar gCal = new GregorianCalendar(  
                    Integer.parseInt(date.substring(0, 4)),   
                    Integer.parseInt(date.substring(5, 7)) - 1,   
                    Integer.parseInt(date.substring(8, 10)));  
            gCal.add(GregorianCalendar.MONTH, i);  
            return sdf_date_format.format(gCal.getTime());  
        } catch (Exception e) {  
            log.debug("DateUtil.addMonth():" + e.toString());
            return getDate();  
        }  
    }
  
    public static String rollDate(Calendar cal, int Type, int Span) {  
        try {  
            String temp = "";  
            Calendar rolcale;  
            rolcale = cal;  
            rolcale.add(Type, Span);  
            temp = sdf_date_format.format(rolcale.getTime());  
            return temp;  
        } catch (Exception e) {  
            return "";  
        }  
    }  
  
    /** 
     * 返回默认的日期格式 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @return 
     */  
    public static synchronized String getDatePattern() {  
        defaultDatePattern = "yyyy-MM-dd";  
        return defaultDatePattern;  
    }  
  
    /** 
     * 将指定日期按默认格式进行格式代化成字符串后输出如：yyyy-MM-dd 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @param aDate 
     * @return 
     */  
    public static final String getDate(Date aDate) {  
        SimpleDateFormat df = null;  
        String returnValue = "";  
        if (aDate != null) {  
            df = new SimpleDateFormat(getDatePattern());  
            returnValue = df.format(aDate);  
        }  
        return (returnValue);  
    }
  
    /** 
     * 将日期类转换成指定格式的字符串形式 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @param aMask 
     * @param aDate 
     * @return 
     */  
    public static final String getDateTime(String aMask, Date aDate) {  
        SimpleDateFormat df = null;  
        String returnValue = "";  
  
        if (aDate == null) {  
            log.error("aDate is null!");
        } else {  
            df = new SimpleDateFormat(aMask);  
            returnValue = df.format(aDate);  
        }  
        return (returnValue);  
    }  
  
    /** 
     * 将指定的日期转换成默认格式的字符串形式 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @param aDate 
     * @return 
     */  
    public static final String convertDateToString(Date aDate) {  
        return getDateTime(getDatePattern(), aDate);  
    }  
  
    /** 
     * 将日期字符串按指定格式转换成日期类型 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @param aMask 指定的日期格式，如:yyyy-MM-dd 
     * @param strDate 待转换的日期字符串 
     * @return 
     * @throws ParseException 
     */  
    public static final Date convertStringToDate(String aMask, String strDate)  
            throws ParseException {  
        SimpleDateFormat df = null;  
        Date date = null;  
        df = new SimpleDateFormat(aMask);  
  
        if (log.isDebugEnabled()) {
            log.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");
        }  
        try {  
            date = df.parse(strDate);  
        } catch (ParseException pe) {  
            log.error("ParseException: " + pe);
            throw pe;  
        }  
        return (date);  
    }  
  
    /** 
     * 将日期字符串按默认格式转换成日期类型 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @param strDate 
     * @return 
     * @throws ParseException 
     */  
    public static Date convertStringToDate(String strDate)  
            throws ParseException {  
        Date aDate = null;  
  
        try {  
            if (log.isDebugEnabled()) {
                log.debug("converting date with pattern: " + getDatePattern());
            }  
            aDate = convertStringToDate(getDatePattern(), strDate);  
        } catch (ParseException pe) {  
            log.error("Could not convert '" + strDate + "' to a date, throwing exception");
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());  
        }  
        return aDate;  
    }  
  
    /** 
     * 返回一个JAVA简单类型的日期字符串 
     * @author dylan_xu 
     * @date Mar 11, 2012 
     * @return 
     */  
    public static String getSimpleDateFormat() {  
        SimpleDateFormat formatter = new SimpleDateFormat();  
        String NDateTime = formatter.format(new Date());  
        return NDateTime;  
    }  

    /** 
     * 为查询日期添加最小时间
     */  
    @SuppressWarnings("deprecation")  
    public static Date addStartTime(Date param) {  
        Date date = param;  
        try {  
            date.setHours(0);  
            date.setMinutes(0);  
            date.setSeconds(0);  
            return date;  
        } catch (Exception ex) {  
            return date;  
        }  
    }  
  
    /** 
     * 为查询日期添加最大时间
     */  
    @SuppressWarnings("deprecation")  
    public static Date addEndTime(Date param) {  
        Date date = param;  
        try {  
            date.setHours(23);  
            date.setMinutes(59);  
            date.setSeconds(0);  
            return date;  
        } catch (Exception ex) {  
            return date;  
        }  
    }

    /**
     * yyyyMMdd 格式时间
     *
     * @return
     */
    public static String getTodayyyyyMMdd() {
        Date date = new Date();
        return sdf_yymmdd_format.format(date);
    }

    public static int getTerm(){
        List<Integer> nextTermList = Arrays.asList(3,4,5,6,7);
        DateTime nowDate = DateTime.now();
        int month = nowDate.getMonthOfYear();
        int day = nowDate.getDayOfMonth();
        return (nextTermList.contains(month) || (month == 2 && day > 15) || (month == 8 && day < 15)) ? 2 : 1;
    }

    public static void main(String[] a){
        System.out.println(DateUtil.addCurrMinute(-30));
        System.out.println(DateUtil.getDateTime());
    }
}  
